<?php
if ( ! function_exists( 'get_tire_manager_var' ) ) {
	function get_tire_manager_var( $var, $first_array_key = false ,$is_number=true) {
		$var = preg_replace( '/^\$/i', '', $var );

		if ( ! isset( ${$var} ) ) {
			require( tiremanager_plugin_dir_path . 'formsconfig.php' );
		}
		if ( ! isset( ${$var} ) ) {
			require( tiremanager_plugin_dir_path . 'sellers_brands.php' );
		}
		$var1 = ${$var};
		if ( $first_array_key && gettype( $var1 ) == 'array' ) {
			if($is_number)
			return intval(array_keys( $var1 )[0]);
			return array_keys( $var1 )[0];
		}
		return $var1;

	}
}
function get_tire_form_info($att) {
	if(!function_exists('tm_specific_times'))
		require( tiremanager_plugin_dir_path . 'tm_shortcode.php' );

	$tire_form_info                            = array(

		'form_id'         => get_tire_manager_var( 'tire_gravity_form_id' ),
		'seller_address'     => get_tire_manager_var( '$tire_gf_fields_seller_address', true ),
		'tire_type_id'     => get_tire_manager_var( '$tire_gf_fields_id_car_type_array', true ),
		'size_id'          => get_tire_manager_var( '$tire_gf_fields_id_tire_sizes', true ),
		'seller_id'          => get_tire_manager_var( '$tire_gf_fields_id_sellers_array', true ),
		'quantity_id'      => get_tire_manager_var( '$tire_gf_fields_id_quantity_asked', true ),
		'brand_id'         => get_tire_manager_var( '$tire_gf_fields_id_brands_array', true ),
		'sellers_list_all' => get_tire_manager_var( 'sellers_array' ),
		'brands_list_all'  => get_tire_manager_var( 'brands_array' ),
	);
	$tire_table_quantities_without_prefix_name = get_tire_manager_var( 'tire_table_quantities_without_prefix_name' );
	global $wpdb;
	$table_viw_name         = $wpdb->prefix . "viw_" . $tire_table_quantities_without_prefix_name;
	$query                  = "SELECT * FROM $table_viw_name WHERE 
                                                            tire_is_active = 1
									and tire_quantity > 0
									";
	$tire_form_info['data'] = $wpdb->get_results( $query );
	$rand                = wp_rand( 1000, 99999 );
	$url = tiremanager_plugin_dir_url;

	return (
		"
		<script src='$url/styles/ts/AvailableTireClass.js'>	
		</script>
		<div id='tire-info-".$rand."'>
		<script>
               	 const tire_info =new TireInfo(JSON.parse('".json_encode($tire_form_info)."'));
    	</script>
    	</div>
                ".tm_specific_times($att)
	);
}
add_shortcode('gettireforminfo','get_tire_form_info');
